﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Web.Mvc;
using Model.WaiGaoQiao;
using WaiGaoQiaoWeb.Models;
using WaiGaoQiaoWeb.JsonEntities;

namespace WaiGaoQiaoWeb.Controllers
{
    public class ConyifuPersonnelInfoController : Controller
    {
        private ConyifuPersonnelInfoInfo perinfo = new ConyifuPersonnelInfoInfo();
        public ActionResult ConyifuPersonnelInfoIndex()
        {
            ViewData["Deptment"] = GetDeptment();
            return View();
        }

        /// <summary>
        /// 获取部门列表（用于绑定下拉框）
        /// </summary>
        /// <returns></returns>
        public IList<SelectListItem> GetDeptment()
        {
            IDictionary<int, string> dic = new Dictionary<int, string>();
            DataSet ds = perinfo.GetdeptName();
            dic.Add(-1, "全部");
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                dic.Add(Convert.ToInt32(ds.Tables[0].Rows[i]["Dep_ID"]), ds.Tables[0].Rows[i]["deptName"].ToString().Trim());
            }

            IList<SelectListItem> selectList =
                dic.Select(m => new SelectListItem() { Text = m.Value.ToString(), Value = m.Key.ToString() }).ToList();
            return selectList;
        }

        /// <summary>
        /// 获取人事信息列表
        /// </summary>
        /// <param name="pvm"></param>
        /// <returns></returns>
        public ActionResult GetPersonnelIinformationList(PageVM pvm)
        {
            string query = " 1=1 ";
            if (string.IsNullOrEmpty(pvm.query))
            {
                query += " and 1=1";
            }
            else
            {
                string condition = pvm.query;
                if (condition.Split(',')[0].ToString() != "" && condition.Split(',')[0].ToString() != null)
                {
                    query += " and PName like'%" + condition.Split(',')[0].ToString() + "%'";
                }
                if (condition.Split(',')[1].ToString() != "-1")
                {
                    query += " and DEP_ID='" + condition.Split(',')[1].ToString() + "'";
                }
                if (condition.Split(',')[2].ToString() != "" && condition.Split(',')[2].ToString() != null)
                {
                    query += " and EMPNO='" + condition.Split(',')[2].ToString() + "'";
                }
                if (condition.Split(',')[3].ToString() != "" && condition.Split(',')[3].ToString() != null)
                {
                    query += " and CARDSERIAL=" + condition.Split(',')[3].ToString();
                }
            }
            IList<ConyifuPersonnelInfoInfo> perlist = perinfo.SelPersonnelIinformationInfo(pvm.rp, pvm.page, query);
            JsonFlexiGridData jfgd = new JsonFlexiGridData();
            jfgd.page = pvm.page;
            jfgd.total = perinfo.GetPersonnelIinformationCount(query);
            List<string> v;
            foreach (ConyifuPersonnelInfoInfo item in perlist)
            {
                #region
                v = new List<string>();
                v.Add(item.PID.ToString());
                v.Add(item.DeptName);
                v.Add(item.EMPNO.ToString());
                v.Add(item.PName.ToString());
                v.Add(item.CARDSERIAL.ToString());
                v.Add(item.BalanceT.ToString());
                v.Add(item.CARDSTATUS.ToString());
                #endregion
                FlexiGridRow fgr = new FlexiGridRow()
                {
                    id = item.PID.ToString(),
                    cell = v
                };
                jfgd.rows.Add(fgr);
            }
            return Json(jfgd);
        }
    }
}
